/**********************************************************************************
// DemoSprite
//
// Creation:	02 Jul 2007
// Updated:		27 May 2011
// Compiler:	Visual Studio 2011
//
// Notes:		Show an image on screen using the sprite class
//
**********************************************************************************/

#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include "Resources.h"
#include "Anabele.h"	


/**********************************************************************************/
/*                            SpriteDemo Class                                    */
/**********************************************************************************/


class SpriteDemo : public D3DGame
{
private:
	float lastx, lasty;
	int dirX, dirY;

	Sprite * img;
	Sprite * demobox;

	char text[80];

public:
	void GameInit();
	void GameLogic();
	void GameDraw();
	void GameShutdown();
	void UnloadResources();
	void LoadResources();
};

/**********************************************************************************/

void SpriteDemo::GameInit()
{
	lastx = (GetWindowWidth()/2.0f);
	lasty = (GetWindowHeight()/2.0f);

	dirX = 5;
	dirY = 5;

	LoadResources();	
}

/**********************************************************************************/

void SpriteDemo::LoadResources() 
{
	img = new Sprite("Resources\\CarKaraCanvas.bmp");
	img->MoveTo(lastx, lasty);

	demobox = new Sprite("Resources/demoBox.png");
	demobox->MoveTo(155, 100);
}							


/**********************************************************************************/

void SpriteDemo::UnloadResources()
{
	lastx = img->x;
	lasty = img->y;

	if (demobox) delete demobox;
	if (img) delete img;
}

/**********************************************************************************/

void SpriteDemo::GameShutdown()
{
	UnloadResources();
}

/**********************************************************************************/

void SpriteDemo::GameLogic()
{
	// for now test if user is hitting ESC and send WM_CLOSE
	EndsOnEscape();

	img->x += dirX;
	img->y += dirY;
	
	if (img->x <= (img->GetWidth()/2.0f))
	{	
		img->x = (img->GetWidth()/2.0f);
		dirX = -dirX;
	}

	if (img->y <= (img->GetHeight()/2.0f))
	{
		img->y = (img->GetHeight()/2.0f);
		dirY = -dirY;
	}

	if (img->x + (img->GetWidth()/2) >= GetWindowWidth())
	{
		img->x = GetWindowWidth() - (img->GetWidth()/2.0f);
		dirX = -dirX;		
	}

	if (img->y + (img->GetHeight()/2) >= GetWindowHeight())
	{
		img->y = GetWindowHeight() - (img->GetHeight()/2.0f);
		dirY = -dirY;				
	}
	
} 

/**********************************************************************************/

void SpriteDemo::GameDraw()
{
	img->Draw();
	demobox->Draw();

	sprintf_s(text, "Sprite Demo");
	sysFont->Draw(60, 45, text);

	sprintf_s(text, "Loading bitmap from disk");
	sysFont->Draw(60, 85, text);

	sprintf_s(text, "Blitting and moving image");
	sysFont->Draw(60, 105, text);

	sprintf_s(text, "Image position: %d x %d", img->IntX(), img->IntY());
	sysFont->Draw(60, 125, text);
} 


/**********************************************************************************/
/*                                  WinMain                                       */
/**********************************************************************************/

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	SpriteDemo sptDemo;

	//sptDemo.SetWindowMode(WINDOWED);
	//sptDemo.SetWindowSize(1024,768);
	sptDemo.SetWindowBgColor(16,16,16);
	sptDemo.SetWindowTitle(string("Sprite Demo"));
	sptDemo.SetWindowIcon(LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)));
	sptDemo.ShowWindowCursor(false);
	sptDemo.Initialize(hInstance);
	sptDemo.Start();
	
	return 0;

}

/**********************************************************************************/

